home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 11111 < prev    next >
Encoding:
Text File  |  1996-08-05  |  1.9 KB  |  67 lines

  1. Path: lerc.nasa.gov!purdue!yuma!steffend
  2. From: steffend@lamar.colostate.edu (Dave Steffen)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: C++ calling Fortran
  5. Date: 12 Mar 1996 18:44:15 GMT
  6. Organization: Colorado State University, Fort Collins, CO  80523
  7. Message-ID: <4i4glv$3foe@yuma.ACNS.ColoState.EDU>
  8. References: <4i46vr$njn@filet.nrlssc.navy.mil>
  9. NNTP-Posting-Host: glitch.physics.colostate.edu
  10. X-Newsreader: TIN [version 1.2 PL2]
  11.  
  12. Gregg A. Jacobs (X4720) (jacobs@proteus) wrote:
  13. > I want a C++ program to call a subroutine written in fortran.
  14.  
  15. [snip]
  16.  
  17. > Then f77 generates an object file with a subroutine name of:
  18.  
  19. > _foo_
  20.  
  21. > A C (or C++) routine calling this would be:
  22.  
  23. > void foo_(float *a);
  24.  
  25. > main() {
  26.  
  27. >    float a;
  28. >    foo_(&a);
  29. > }
  30.  
  31. > The gcc compiler then generates an object file which looks for
  32. > the name:
  33. > _foo_
  34. > This is fine, and the C program calls the fortran subroutine
  35.  
  36. > However, g++ generates an object file which looks for the name:
  37. > _foo___FPf
  38. > The linker can not find this, so the C++ code will not call
  39. > the fortran subroutine.
  40.  
  41.  
  42.     Right, the C++ compiler will mangle the name of this
  43. subroutine, just like it mangles the name of every other function
  44. call. What you need to do is tell g++ "Don't Mangle that Name!" by
  45. declaring it to have C linkage. Try declaring your routine like this:
  46.  
  47. extern "C" {
  48.  
  49.     void foo_ (float* a);
  50. }
  51.  
  52.  
  53.     This tells g++ not to mangle the name, and everything should
  54. work out. Let me know if it doesn't.
  55.  
  56.                                  /\
  57.                                  \/
  58.  
  59. Dave Steffen                      No, his mind is not for rent
  60. Dept. of Physics                  To any God or Government
  61. Colorado State University         Always hopeful, yet discontent
  62. steffend@lamar.colostate edu      He knows changes aren't permanent-
  63.                       But change is...
  64. "Speak softly...                    
  65. ... and carry a black belt!"              -Neal Peart / RUSH
  66. -----------------------------------------------------------------------
  67.